﻿#region DaNotice
/*----------------------------------------------------------------
// 文件名：DaNotice.cs
// 功能描述：表dbo.Notice的数据访问层
//
// 
// 创建时间：2009-11-20 DtataAccess template . Ver 5.0.20090413
//
// 修改标识： 
// 修改描述： 
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using EQT.Model;


namespace EQT.Dal
{
    ///可以继承更上层接口,INotice ,数据访问层基类DaBase，
    ///DaBase中包含了DbException异常处理静态方法
    public partial class DaNotice : DaBase<MoNotice>
    {
        #region SQL const
        internal const string TABLE_COLUMNS = " NOTICE_ID ,UID ,PUBLISH_TYPE ,NOTICE_CONTENT ,CREATE_TIME ,TITLE   ";

        internal const string SQL_INSERT = "INSERT INTO NOTICE (" + TABLE_COLUMNS + ") VALUES (@NoticeId,@Uid,@PublishType,@NoticeContent,@CreateTime,@Title)";
        internal const string SQL_SELECT = "SELECT " + TABLE_COLUMNS + " FROM NOTICE  ";
        internal const string SQL_SELECT_ONE = SQL_SELECT + " WHERE NOTICE_ID=@NoticeId";

        internal const string SQL_EXIST = "SELECT COUNT(*) FROM NOTICE  WHERE NOTICE_ID=@NoticeId  ";
        internal const string SQL_UPDATE = "UPDATE NOTICE SET UID=@Uid, PUBLISH_TYPE=@PublishType, NOTICE_CONTENT=@NoticeContent, CREATE_TIME=@CreateTime, TITLE=@Title WHERE NOTICE_ID=@NoticeId";

        internal const string SQL_DELETE_DEFAULT = "DELETE FROM NOTICE ";
        internal const string SQL_DELETE = "DELETE FROM NOTICE WHERE NOTICE_ID=@NoticeId";

        internal const string SQL_COUNT = "SELECT COUNT(*) FROM Notice ";
        #endregion

        #region Constructor

        /// <summary>
        /// 默认构造函数，使用配置文件中默认的数据库配置。
        /// </summary>
        public DaNotice()
        {
            this.db = DatabaseFactory.CreateDatabase();
        }
        /// <summary>
        /// 使用指定的databaseName数据库配置。
        /// </summary>
        /// <param name="databaseName">配置文件中数据库的配置名称。</param>
        public DaNotice(string databaseName)
        {
            this.db = DatabaseFactory.CreateDatabase(databaseName);
        }

        public DaNotice(Database db)
        {
            this.db = db;
        }

        #endregion

        //以下是CRUD方法，每个方法都有引入外部事务的版本

        #region 构造器模块

        internal override MoNotice ConstructT()
        {
            return new MoNotice();
        }
        #endregion



        #region Add模块 Helper

        ///build the command object.It never throw exception.
        internal override DbCommand ConstructAddCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);

            db.AddInParameter(dbCommand, "NoticeId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString);
            db.AddInParameter(dbCommand, "PublishType", DbType.Int32);
            db.AddInParameter(dbCommand, "NoticeContent", DbType.String);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime);
            db.AddInParameter(dbCommand, "Title", DbType.String);
            return dbCommand;
        }
        internal override DbCommand ConstructAddCommand(Database db, MoNotice entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);
            db.AddInParameter(dbCommand, "NoticeId", DbType.AnsiString, entity.NoticeId);
            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString, entity.Uid);
            db.AddInParameter(dbCommand, "PublishType", DbType.Int32, entity.PublishType);
            db.AddInParameter(dbCommand, "NoticeContent", DbType.String, entity.NoticeContent);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime, entity.CreateTime);
            db.AddInParameter(dbCommand, "Title", DbType.String, entity.Title);
            return dbCommand;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, MoNotice entity)
        {
            db.SetParameterValue(addCmd, "NoticeId", entity.NoticeId);
            db.SetParameterValue(addCmd, "Uid", entity.Uid);
            db.SetParameterValue(addCmd, "PublishType", entity.PublishType);
            db.SetParameterValue(addCmd, "NoticeContent", entity.NoticeContent);
            db.SetParameterValue(addCmd, "CreateTime", entity.CreateTime);
            db.SetParameterValue(addCmd, "Title", entity.Title);
            return addCmd;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, DataRow row)
        {
            db.SetParameterValue(addCmd, "NoticeId", row["NoticeId"]);
            db.SetParameterValue(addCmd, "Uid", row["Uid"]);
            db.SetParameterValue(addCmd, "PublishType", row["PublishType"]);
            db.SetParameterValue(addCmd, "NoticeContent", row["NoticeContent"]);
            db.SetParameterValue(addCmd, "CreateTime", row["CreateTime"]);
            db.SetParameterValue(addCmd, "Title", row["Title"]);
            return addCmd;
        }

        #endregion

        #region Update模块 Helper

        internal override DbCommand ConstructUpdateCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString);
            db.AddInParameter(dbCommand, "PublishType", DbType.Int32);
            db.AddInParameter(dbCommand, "NoticeContent", DbType.String);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime);
            db.AddInParameter(dbCommand, "Title", DbType.String);
            db.AddInParameter(dbCommand, "NoticeId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructUpdateCommand(Database db, MoNotice entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "Uid", DbType.AnsiString, entity.Uid);
            db.AddInParameter(dbCommand, "PublishType", DbType.Int32, entity.PublishType);
            db.AddInParameter(dbCommand, "NoticeContent", DbType.String, entity.NoticeContent);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime, entity.CreateTime);
            db.AddInParameter(dbCommand, "Title", DbType.String, entity.Title);
            db.AddInParameter(dbCommand, "NoticeId", DbType.AnsiString, entity.NoticeId);

            return dbCommand;
        }

        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, MoNotice entity)
        {
            db.SetParameterValue(updateCmd, "Uid", entity.Uid);
            db.SetParameterValue(updateCmd, "PublishType", entity.PublishType);
            db.SetParameterValue(updateCmd, "NoticeContent", entity.NoticeContent);
            db.SetParameterValue(updateCmd, "CreateTime", entity.CreateTime);
            db.SetParameterValue(updateCmd, "Title", entity.Title);
            db.SetParameterValue(updateCmd, "NoticeId", entity.NoticeId);
            return updateCmd;
        }
        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, DataRow row)
        {
            db.SetParameterValue(updateCmd, "Uid", row["Uid"]);
            db.SetParameterValue(updateCmd, "PublishType", row["PublishType"]);
            db.SetParameterValue(updateCmd, "NoticeContent", row["NoticeContent"]);
            db.SetParameterValue(updateCmd, "CreateTime", row["CreateTime"]);
            db.SetParameterValue(updateCmd, "Title", row["Title"]);
            db.SetParameterValue(updateCmd, "NoticeId", row["NoticeId"]);
            return updateCmd;
        }

        internal override void PrepareDataAdapterCommand(Database db, out DbCommand dbInsertCommand,
                             out DbCommand dbUpdateCommand, out DbCommand dbDeleteCommand)
        {
            dbInsertCommand = db.GetSqlStringCommand(SQL_INSERT);
            #region set insert cmd parameters
            db.AddInParameter(dbInsertCommand, "NoticeId", DbType.AnsiString, "NOTICE_ID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Uid", DbType.AnsiString, "UID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "PublishType", DbType.Int32, "PUBLISH_TYPE", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "NoticeContent", DbType.String, "NOTICE_CONTENT", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "CreateTime", DbType.DateTime, "CREATE_TIME", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Title", DbType.String, "TITLE", DataRowVersion.Current);
            #endregion
            dbUpdateCommand = db.GetSqlStringCommand(SQL_UPDATE);
            #region Set update cmd value parameters
            db.AddInParameter(dbUpdateCommand, "Uid", DbType.AnsiString, "UID", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "PublishType", DbType.Int32, "PUBLISH_TYPE", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "NoticeContent", DbType.String, "NOTICE_CONTENT", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "CreateTime", DbType.DateTime, "CREATE_TIME", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Title", DbType.String, "TITLE", DataRowVersion.Current);
            #endregion
            #region set update cmd pk where parameters
            db.AddInParameter(dbUpdateCommand, "NoticeId", DbType.AnsiString, "NOTICE_ID", DataRowVersion.Current);
            #endregion
            dbDeleteCommand = db.GetSqlStringCommand(SQL_DELETE);
            #region set delete cmd pk where parameters
            db.AddInParameter(dbDeleteCommand, "NoticeId", DbType.AnsiString, "NOTICE_ID", DataRowVersion.Current);
            #endregion
        }
        #endregion

        #region Delete模块 Helper

        internal override DbCommand ConstructDeleteCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "NoticeId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructDeleteCommand(Database db, MoNotice entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "NoticeId", DbType.AnsiString, entity.NoticeId);

            return dbCommand;
        }

        internal override DbCommand ConstructDeleteCommandForWhere(Database db, string where)
        {
            return db.GetSqlStringCommand(SQL_DELETE_DEFAULT + where);
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, MoNotice entity)
        {
            db.SetParameterValue(deleteCmd, "NoticeId", entity.NoticeId);
            return deleteCmd;
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, DataRow row)
        {
            db.SetParameterValue(deleteCmd, "NoticeId", row["NoticeId"]);
            return deleteCmd;
        }

        #endregion helper

        #region Query模块 Helper

        internal override DbCommand ConstructQueryCommand(string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region GetEntity(s)模块 Helper

        internal override DbCommand ConstructSelectOneCommand(Database db, MoNotice entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_SELECT_ONE);

            db.AddInParameter(dbCommand, "NoticeId", DbType.AnsiString, entity.NoticeId);
            return dbCommand;
        }

        internal override void PopulateEntityByDataReader(IDataReader reader, ref MoNotice entity)
        {
            if (!reader.IsDBNull(0)) entity.NoticeId = reader.GetString(0);
            if (!reader.IsDBNull(1)) entity.Uid = reader.GetString(1);
            if (!reader.IsDBNull(2)) entity.PublishType = reader.GetInt32(2);
            if (!reader.IsDBNull(3)) entity.NoticeContent = reader.GetString(3);
            if (!reader.IsDBNull(4)) entity.CreateTime = reader.GetDateTime(4);
            if (!reader.IsDBNull(5)) entity.Title = reader.GetString(5);
        }



        internal override DbCommand ConstructSelectConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region Count模块 Helper

        internal override DbCommand ConstructCountCommand(Database db)
        {
            return db.GetSqlStringCommand(SQL_COUNT);
        }

        internal override DbCommand ConstructCountConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_COUNT + condition);
        }

        #endregion

        #region IsExist模块 Helper

        internal override DbCommand ConstructIsExistCommand(Database db, MoNotice entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_EXIST);
            db.AddInParameter(dbCommand, "NoticeId", DbType.AnsiString, entity.NoticeId);
            return dbCommand;
        }

        #endregion



    }
}
#endregion

